package com.press.controllers.blog;

import com.press.bean.ContentBean;
import com.press.mapper.ExampleMapper;
import com.press.services.ExampleService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by haocongshun on 2016/10/18.
 */
@Controller
@RequestMapping("/blog")
public class ExampleController {
    public static int pageNow=1;
    public static int pageCount;
    private static Logger log = Logger.getLogger(ExampleController.class);
    @Resource(name = "exampleService")
    private ExampleService exampleService;

    public ExampleService getExampleService() {
        return exampleService;
    }

    public void setExampleService(ExampleService exampleService) {
        this.exampleService = exampleService;
    }
    @ModelAttribute
    public void init(){
        pageNow=1;
        pageCount=exampleService.getPageCount();
    }
    @RequestMapping("/example")
    public void example(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        handlePage(request,response);
        request.setAttribute("pages",getPage(request));
        request.setAttribute("activePage",pageNow);
        log.info(pageNow);
        Map<String,String> option = (Map<String ,String>)request.getServletContext().getAttribute("Option");
        request.setAttribute("title",option.get("web_name")+",欢迎访问！");
        String forwardUrl = "/"+option.get("web_template_id")+"/example.jsp";
        request.setAttribute("contents",getContent(request));
        request.getRequestDispatcher(forwardUrl).forward(request,response);
    }
    private Map<String,ContentBean> getContent(HttpServletRequest request){
        List<ContentBean> listContent = exampleService.getContent(pageNow);
        Map<String,ContentBean> mapContent = new HashMap<>();
        String url = request.getContextPath()+"/content?content_id=";
        for (ContentBean content : listContent){
            content.setContent_thumbnail(request.getContextPath()+content.getContent_thumbnail());
            mapContent.put(url+content.getContent_id(),content);
        }
        return mapContent;
    }
    public void handlePage(HttpServletRequest request, HttpServletResponse response){
        String page = request.getParameter("page");
        log.info(page);
        if(page==null){
            pageNow=1;
        }else if(page.equals("next")){
            if(pageNow<pageCount){
                pageNow++;
            }
        }else if(page.equals("last")){
            if(pageNow>1){
                pageNow--;
            }
        }else {
            log.info(page);
            pageNow=Integer.parseInt(page);
            log.info(pageNow);
        }
    }
    private Map<String,String> getPage(HttpServletRequest request){
        Map<String,String> mapPage = new LinkedHashMap<>();
        String url = request.getContextPath()+"/blog/example.press?page=";
        mapPage.put(url+"last","<<");
        for(int i = 1;i<=pageCount;i++){
            mapPage.put(url+i,String.valueOf(i));
        }
        mapPage.put(url+"next",">>");
        return mapPage;
    }
}
